﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class Controls_COMMODITY_AUTO : System.Web.UI.UserControl
{
    public string SPDM
    {
        set { HiddenField2.Value = value; }
        get { return HiddenField2.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    SHUXINGBINDER();
        //}
        
    }
    
    public void Bind(string spdm)
    {
        HiddenField2.Value = spdm;
        if (new BLL.COMMODITY_AUTO().Exists(spdm))
        {
            HiddenField1.Value = "1";
            Model.COMMODITY_AUTO ca = new BLL.COMMODITY_AUTO().GetModel(spdm);
            TextBox1.Text = ca.CA_NAME;
            TextBox2.Text = ca.CA_WEIGHT.ToString();
            //CheckBox8.Checked = ca.CA_AGAIN == 1 ? true : false;
            CheckBox3.Checked = ca.CA_NEW == 1 ? true : false;
            CheckBox4.Checked = ca.CA_RCMD == 1 ? true : false;
            CheckBox5.Checked = ca.CA_OFFER == 1 ? true : false;
            CheckBox6.Checked = ca.CA_GIFT == 1 ? true : false;
            //CheckBox7.Checked = ca.CA_PREPUP == 1 ? true : false;
            DropDownList1.SelectedValue = ca.CA_AUTOPROP1 == "" ? null : ca.CA_AUTOPROP1;
            DropDownList2.SelectedValue = ca.CA_AUTOPROP2 == "" ? null : ca.CA_AUTOPROP2;
            DropDownList3.SelectedValue = ca.CA_AUTOPROP3 == "" ? null : ca.CA_AUTOPROP3;
            DropDownList4.SelectedValue = ca.CA_AUTOPROP4 == "" ? null : ca.CA_AUTOPROP4;
            DropDownList5.SelectedValue = ca.CA_AUTOPROP5 == "" ? null : ca.CA_AUTOPROP5;
            DropDownList6.SelectedValue = ca.CA_AUTOPROP6 == "" ? null : ca.CA_AUTOPROP6;
            m_myWebTextPane.Text = ca.CA_NOTE;
            txtMaterial.Text = ca.CA_FIELD1;

            try
            {
                Model.SYSPARAM sysparam = new BLL.SYSPARAM().GetModel("012");

                string tmp = "";
                fj1.Text = ca.BYZD3;
                tmp = sysparam.SP_PARAM13.Trim();
                if (tmp == "")
                {
                    tmp = "备注1";
                }
                bz1.Text = tmp;


                fj2.Text = ca.BYZD4;
                tmp = sysparam.SP_PARAM14.Trim();
                if (tmp == "")
                {
                    tmp = "备注2";
                }
                bz2.Text = tmp;

                fj3.Text = ca.BYZD5;
                tmp = sysparam.SP_PARAM15.Trim();
                if (tmp == "")
                {
                    tmp = "备注3";
                }
                bz3.Text = tmp;

                fj4.Text = ca.BYZD6;
                tmp = sysparam.SP_PARAM16.Trim();
                if (tmp == "")
                {
                    tmp = "备注4";
                }
                bz4.Text = tmp;
            }

            catch {
                Share.Alert(this.Page.Page, "出错了,请先初始化sysparam表的参数");
            }


            

            ////m_myWebTextPane.XMLNText = "adfa";
            ////Share.Alert(this.Page,ca.CA_NOTE); Share.Alert(this.Page,m_myWebTextPane.XMLNText);
        }
        else
        {
            m_myWebTextPane.Text = String.Empty;
            TextBox1.Text = String.Empty;
            TextBox2.Text = String.Empty;
            CheckBox3.Checked = false;
            CheckBox4.Checked = false;
            CheckBox5.Checked = false;
            CheckBox6.Checked = false;
            //CheckBox7.Checked = false;
        }
    }

    public void SHUXINGBINDER()
    {
        if (Share.GetIsDynamic() == "0") //固定属性

        {
            
                Model.SYSPARAM syspa = new BLL.SYSPARAM().GetModel("002");
                Label3.Text = (syspa.SP_PARAM13 == null ? "扩展属性1" : syspa.SP_PARAM13);
                Label4.Text = (syspa.SP_PARAM14 == null ? "扩展属性2" : syspa.SP_PARAM14);
                Label5.Text = (syspa.SP_PARAM15 == null ? "扩展属性3" : syspa.SP_PARAM15);
                Label6.Text = (syspa.SP_PARAM16 == null ? "扩展属性4" : syspa.SP_PARAM16);
                Label7.Text = (syspa.SP_PARAM17 == null ? "扩展属性5" : syspa.SP_PARAM17);
                Label8.Text = (syspa.SP_PARAM18 == null ? "扩展属性6" : syspa.SP_PARAM18);
                List<Model.AUTOPROP1> a1 = new BLL.AUTOPROP1().GetAllList();
                DropDownList1.DataSource = a1;
                DropDownList1.DataBind();
                List<Model.AUTOPROP2> a2 = new BLL.AUTOPROP2().GetAllList();
                DropDownList2.DataSource = a2;
                DropDownList2.DataBind();
                List<Model.AUTOPROP3> a3 = new BLL.AUTOPROP3().GetAllList();
                DropDownList3.DataSource = a3;
                DropDownList3.DataBind();
                List<Model.AUTOPROP4> a4 = new BLL.AUTOPROP4().GetAllList();
                DropDownList4.DataSource = a4;
                DropDownList4.DataBind();
                List<Model.AUTOPROP5> a5 = new BLL.AUTOPROP5().GetAllList();
                DropDownList5.DataSource = a5;
                DropDownList5.DataBind();
                List<Model.AUTOPROP6> a6 = new BLL.AUTOPROP6().GetAllList();
                DropDownList6.DataSource = a6;
                DropDownList6.DataBind();
          
        }
        else  //动态属性

        {
            if (HiddenField2.Value.Length > 0)
            {
                Model.TYLX_SHANGPIN ty_sp = new BLL.TYLX_SHANGPIN().GetModel(HiddenField2.Value);
                if (ty_sp != null)
                {
                    DataTable lx_sx = new BLL.TYLX_SHUXING().GetTYLXPropertyData(ty_sp.LXID);
                    Label3.Text = "扩展属性1";
                    Label4.Text = "扩展属性2";
                    Label5.Text = "扩展属性3";
                    Label6.Text = "扩展属性4";
                    Label7.Text = "扩展属性5";
                    Label8.Text = "扩展属性6";
                    DropDownList1.Enabled = false;
                    DropDownList2.Enabled = false;
                    DropDownList3.Enabled = false;
                    DropDownList4.Enabled = false;
                    DropDownList5.Enabled = false;
                    DropDownList6.Enabled = false;
                    StringBuilder Sb;
                    DataTable dt;
                    foreach (DataRow row in lx_sx.Rows)
                    {
                        #region
                        switch (row["SX_TABLE"].ToString())
                        {
                            case "AUTOPROP1":
                                Label3.Text = row["SX_MC"].ToString();
                                Sb = new StringBuilder();
                                Sb.Append("select MXID,SXID,TableName,AP1_CODE,AP1_Name from ");
                                Sb.Append("SXMX A left join AUTOPROP1 B on A.SXID=B.AP1_CODE ");
                                Sb.Append("where A.TableName='AUTOPROP1' AND A.[mxID]=" + row["SXID"].ToString());
                                dt = new BLL.TYLX_SHUXING().GetSXList(Sb.ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    DropDownList1.Items.Clear();
                                    DropDownList1.Items.Add(new ListItem("请选择", "0"));
                                    DropDownList1.AppendDataBoundItems = true;
                                    DropDownList1.DataSource = dt;
                                    DropDownList1.DataTextField = "AP1_Name";
                                    DropDownList1.DataValueField = "AP1_CODE";
                                    DropDownList1.DataBind();
                                    DropDownList1.Enabled = true;
                                }
                                break;
                            case "AUTOPROP2":
                                Label4.Text = row["SX_MC"].ToString();
                                Sb = new StringBuilder();
                                Sb.Append("select MXID,SXID,TableName,AP2_CODE,AP2_Name from ");
                                Sb.Append("SXMX A left join AUTOPROP2 B on A.SXID=B.AP2_CODE ");
                                Sb.Append("where A.TableName='AUTOPROP2'");
                                dt = new BLL.TYLX_SHUXING().GetSXList(Sb.ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    DropDownList2.Items.Clear();
                                    DropDownList2.Items.Add(new ListItem("请选择", "0"));
                                    DropDownList2.AppendDataBoundItems = true;
                                    DropDownList2.DataSource = dt;
                                    DropDownList2.DataTextField = "AP2_Name";
                                    DropDownList2.DataValueField = "AP2_CODE";
                                    DropDownList2.DataBind();
                                    DropDownList2.Enabled = true;
                                }
                                break;
                            case "AUTOPROP3":
                                Label5.Text = row["SX_MC"].ToString();
                                Sb = new StringBuilder();
                                Sb.Append("select MXID,SXID,TableName,AP3_CODE,AP3_Name from ");
                                Sb.Append("SXMX A left join AUTOPROP3 B on A.SXID=B.AP3_CODE ");
                                Sb.Append("where A.TableName='AUTOPROP3'");
                                dt = new BLL.TYLX_SHUXING().GetSXList(Sb.ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    DropDownList3.Items.Clear();
                                    DropDownList3.Items.Add(new ListItem("请选择", "0"));
                                    DropDownList3.AppendDataBoundItems = true;
                                    DropDownList3.DataSource = dt;
                                    DropDownList3.DataTextField = "AP3_Name";
                                    DropDownList3.DataValueField = "AP3_CODE";
                                    DropDownList3.DataBind();
                                    DropDownList3.Enabled = true;
                                }
                                break;
                            case "AUTOPROP4":
                                Label6.Text = row["SX_MC"].ToString();
                                Sb = new StringBuilder();
                                Sb.Append("select MXID,SXID,TableName,AP4_CODE,AP4_Name from ");
                                Sb.Append("SXMX A left join AUTOPROP4 B on A.SXID=B.AP4_CODE ");
                                Sb.Append("where A.TableName='AUTOPROP4'");
                                dt = new BLL.TYLX_SHUXING().GetSXList(Sb.ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    DropDownList4.Items.Clear();
                                    DropDownList4.Items.Add(new ListItem("请选择", "0"));
                                    DropDownList4.AppendDataBoundItems = true;
                                    DropDownList4.DataSource = dt;
                                    DropDownList4.DataTextField = "AP4_Name";
                                    DropDownList4.DataValueField = "AP4_CODE";
                                    DropDownList4.DataBind();
                                    DropDownList4.Enabled = true;
                                }
                                break;
                            case "AUTOPROP5":
                                Label7.Text = row["SX_MC"].ToString();
                                Sb = new StringBuilder();
                                Sb.Append("select MXID,SXID,TableName,AP5_CODE,AP5_Name from ");
                                Sb.Append("SXMX A left join AUTOPROP5 B on A.SXID=B.AP5_CODE ");
                                Sb.Append("where A.TableName='AUTOPROP5'");
                                dt = new BLL.TYLX_SHUXING().GetSXList(Sb.ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    DropDownList5.Items.Clear();
                                    DropDownList5.Items.Add(new ListItem("请选择", "0"));
                                    DropDownList5.AppendDataBoundItems = true;
                                    DropDownList5.DataSource = dt;
                                    DropDownList5.DataTextField = "AP5_Name";
                                    DropDownList5.DataValueField = "AP5_CODE";
                                    DropDownList5.DataBind();
                                    DropDownList5.Enabled = true;
                                }
                                break;
                            case "AUTOPROP6":
                                Label8.Text = row["SX_MC"].ToString();
                                Sb = new StringBuilder();
                                Sb.Append("select MXID,SXID,TableName,AP6_CODE,AP6_Name from ");
                                Sb.Append("SXMX A left join AUTOPROP6 B on A.SXID=B.AP6_CODE ");
                                Sb.Append("where A.TableName='AUTOPROP6'");
                                dt = new BLL.TYLX_SHUXING().GetSXList(Sb.ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    DropDownList6.Items.Clear();
                                    DropDownList6.Items.Add(new ListItem("请选择", "0"));
                                    DropDownList6.AppendDataBoundItems = true;
                                    DropDownList6.DataSource = dt;
                                    DropDownList6.DataTextField = "AP6_Name";
                                    DropDownList6.DataValueField = "AP6_CODE";
                                    DropDownList6.DataBind();
                                    DropDownList6.Enabled = true;
                                }
                                break;

                        }
                        #endregion
                    }
                    Button1.Enabled = true;
                }
                else
                {
                    Share.Alert(this.Page.Page, "请先设置商品类型");
                    Button1.Enabled = false;
                }
            }
            else
            {
                Share.Alert(this.Page.Page, "请先设置商品类型");
                Button1.Enabled = false;
            }
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300100", "00017"))
        {
            Share.Alert(this.Page.Page, "无权操作");
        }
        Model.COMMODITY_AUTO ca = new BLL.COMMODITY_AUTO().GetModel(HiddenField2.Value);
        ca.CA_CODE = HiddenField2.Value;
        ca.CA_NAME = TextBox1.Text;
        ca.CA_WEIGHT = decimal.Parse(TextBox2.Text.Trim());
        //ca.CA_AGAIN = CheckBox8.Checked == true ? 1 : 0;
        ca.CA_NEW = CheckBox3.Checked == true ? 1 : 0;
        ca.CA_RCMD = CheckBox4.Checked == true ? 1 : 0;
        ca.CA_OFFER = CheckBox5.Checked == true ? 1 : 0;
        ca.CA_GIFT = CheckBox6.Checked == true ? 1 : 0;
        //ca.CA_PREPUP = CheckBox7.Checked == true ? 1 : 0;
        ca.CA_AUTOPROP1 = DropDownList1.SelectedValue;
        ca.CA_AUTOPROP2 = DropDownList2.SelectedValue;
        ca.CA_AUTOPROP3 = DropDownList3.SelectedValue;
        ca.CA_AUTOPROP4 = DropDownList4.SelectedValue;
        ca.CA_AUTOPROP5 = DropDownList5.SelectedValue;
        ca.CA_AUTOPROP6 = DropDownList6.SelectedValue;
        ca.CA_NOTE = m_myWebTextPane.Text;
        ca.BYZD3 = fj1.Text;
        ca.BYZD4 = fj2.Text;
        ca.BYZD5 = fj3.Text;
        ca.BYZD6 = fj4.Text;

        ca.CA_FIELD1 = txtMaterial.Text.Trim();
        //商品目录维护时如果没有选择分类，在保存时提示(其他档案)
        bool flag = new BLL.COMMODITY_CT().Exists(HiddenField2.Value);
        if (!flag)
        {
            Share.Alert(Page, "保存成功！请点击“商品分类”进行修改！");

        }
        if (HiddenField1.Value == "1")
        {
            if (new BLL.COMMODITY_AUTO().Update(ca))
            {
                Bind(HiddenField2.Value);
            }
            else
            {
                Share.Alert(this.Page, "操作失败");
            }
        }
        else
        {
            if (new BLL.COMMODITY_AUTO().Add(ca) == null)
            {
                Share.Alert(this.Page, "操作失败");
            }
        }
    }

    //protected void CheckBox7_CheckedChanged(object sender, EventArgs e)
    //{
    //    if (CheckBox7.Checked)
    //    {
    //        if ((new BLL.COMMODITY_AUTO().GetModel(SPDM)).CA_UP == 1)
    //        {
    //            CheckBox7.Checked = false;
    //            Share.Alert(this.Page, "该商品已经上架,不能预上架!");
    //            return;
    //        }

    //        if (new BLL.COMMODITY_PIC().CheckDefaultExists(SPDM))
    //        {
    //            if (new BLL.COMMODITY_CT().SP_COMMODITY_CT_GetByCMODCODE(SPDM) > 0)
    //            {

    //            }
    //            else
    //            {
    //                CheckBox7.Checked = false;
    //                Share.Alert(this.Page, "该商品没有被分类,不能预上架!");
    //                return;
    //            }
    //        }
    //        else
    //        {
    //            CheckBox7.Checked = false;
    //            Share.Alert(this.Page, "该商品未上传默认图片,不能预上架!");
    //            return;
                
    //        }
    //    }
    //}
}
